//
//  HCAppointmentViewController.m
//  HappyChildren
//
//  Created by WillyZhao on 2016/12/16.
//  Copyright © 2016年 WillyZhao. All rights reserved.
//

#import "HCAppointmentViewController.h"

#import "HCAppointView.h"

#import "HCLocationModel.h"

#import "HCAppointmentModel.h"

#import "HCLogViewController.h"

#import "HCBottomTableViewCell.h"

@interface HCAppointmentViewController () <HCAppointViewDelegate,UITableViewDelegate,UITableViewDataSource,HCLocationDelegate> {
    HCAppointmentModel *app_model;
    HCAppointView *app;
    
    NSString *lng_ll;
    NSString *lat_ll;
    NSString *city_ll;
    
    NSArray *tableViews;
    UITableView *tableView_;
}

@end

@implementation HCAppointmentViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.textTitle = @"预约";
    
    //        [WZNetWorkStatusManager show:centerView type:WZNetWorkStatusLoadingWithImage msg:YBKLoadingText subMsg:nil offsetY:(centerView.frame.size.height - YBKAdaptiveH(120))/2.0 failClickBlock:nil];
    
    //初始化页面
    [self loadContentView];
    
    self.dataRequest.delegate = self;
    
    HCLocationModel *model = [[HCLocationModel alloc] init];
    model.delegate = self;
}

-(void)hc_appointmentLat:(NSString *)lat longitude:(NSString *)lng city:(NSString *)city {
    NSLog(@"预约数据:%@,%@,%@",lat,lng,city);
    
    
    //    if ([PublicFunction isNull:HCGETUSER(@"userid")]) {
    //        HCLogViewController *log = [[HCLogViewController alloc] init];
    //
    //        UINavigationController *na = [[UINavigationController alloc] initWithRootViewController:log];
    //        na.navigationBarHidden = YES;
    //        [self.navigationController presentViewController:na animated:YES completion:nil];
    //    }else{
    lng_ll = [NSString stringWithFormat:@"%@",lng];
    lat_ll = [NSString stringWithFormat:@"%@",lat];
    city_ll = [NSString stringWithFormat:@"%@",city];
    [self.dataRequest postLocationAppointment:city userID:HCGETUSER(@"userid") city_lat:lng city_lng:lat];
    //    }
    
}

#pragma mark 网络请求

-(void)dataRequesterFinish:(DataRequester *)requester requesterWithType:(DataRequestType)aType receiveDictionary:(NSDictionary *)dictionary {
    app_model = [[HCAppointmentModel alloc] initWithDictionary:dictionary error:nil];
    [tableView_ reloadData];
    
}

-(void)dataRequesterFail:(DataRequester *)requester requesterWithType:(DataRequestType)aType receiveErrorDictionary:(NSDictionary *)dictionary {
    NSLog(@"数据请求失败，预约");
}


-(void)loadContentView {
    app = [[HCAppointView alloc] initWithFrame:CGRectMake(0, 0, self.centerView.frame.size.width, self.centerView.frame.size.height)];
    app.delegate = self;
    [self.centerView addSubview:app];
    
    //初始化内容视图
    tableView_ = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, self.centerView.frame.size.width, self.centerView.frame.size.height)];
    tableView_.delegate = self;
    tableView_.dataSource = self;
    tableView_.separatorStyle = UITableViewCellSeparatorStyleNone;
    tableView_.rowHeight = 90;
    [self.centerView addSubview:tableView_];
    
    [tableView_ registerNib:[UINib nibWithNibName:@"HCBottomTableViewCell" bundle:[NSBundle mainBundle]] forCellReuseIdentifier:@"cellhomebottom"];
    
}

#pragma mark UITableViewDelegate
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if (app_model) {
        return app_model.index.count;
    }else{
        return 0;
    }
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    HCBottomTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cellhomebottom"];
    if (!cell) {
        cell = [[HCBottomTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cellhomebottom"];
    }
    [cell cellModel:app_model.index[indexPath.row]];
    return cell;
}


-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    HCAppointmentData *data = app_model.index[indexPath.row];
    
    [self pushChildrenViewController:@"HCADetailViewController" parameterObject:data.hc_id dataObject:nil];
}

#pragma mark HCAppointViewDelegate
-(void)appointIndexClicked:(NSIndexPath *)indexPath andDate:(NSString *)date{
    
    HCAppointmentData *data = app_model.index[indexPath.row];
    
    [self pushChildrenViewController:@"HCADetailViewController" parameterObject:data.hc_id dataObject:date];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark 刷新控件
-(void)appointmentViewEndRefresh:(NSArray *)tableView {
    
    tableViews = [NSArray arrayWithArray:tableView];
    
    if ([PublicFunction isNull:city_ll]) {
        return;
    }
    
    if ([PublicFunction isNull:lng_ll]) {
        return;
    }
    
    if ([PublicFunction isNull:lat_ll]) {
        return;
    }
    
    [self.dataRequest postLocationAppointment:city_ll userID:HCGETUSER(@"userid") city_lat:lng_ll city_lng:lat_ll];
    
    
}

/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end
